﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Business.Sales;
using ESOL.Common;
using ESOL.Common.Entity;

namespace ESOL.WinUI.Sales
{
    public partial class frmManageDeliveryFeedback : BaseForm.frmBaseMdiBusinessForm
    {
        #region vars
        private SalesDeliveryFeedbackEntity mSelectedFeedback;
        #endregion

        #region ctors
        public frmManageDeliveryFeedback()
        {
            InitializeComponent();
        }
        #endregion

        #region functions
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "STATUS", true);

            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colDELIVERY_TYPE, "DW", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colSTATUS, "STATUS", true);
        }
        private string GetCondition()
        {
            string condition = "";
            if (string.IsNullOrEmpty(txtFeedbackCode.Text) == false)
            {
                condition += " AND DF.FEEDBACK_CODE LIKE '%" + txtFeedbackCode.Text + "%'";
            }
            if (dtpDeliveryDate.EditValue!=null)
            {
                condition += " AND DF.DELIVERY_DATE >= CONVERT(DATETIME,'" + dtpDeliveryDate.EditValue.ToString() + "')";
            }
            if (dtpDeliveryDateTo.EditValue != null)
            {
                condition += " AND DF.DELIVERY_DATE <= CONVERT(DATETIME,'" + dtpDeliveryDateTo.EditValue.ToString() + "')";
            }
            if (dtpReceivedDate.EditValue != null)
            {
                condition += " AND DF.RECEIVED_DATE >= CONVERT(DATETIME,'" + dtpReceivedDate.EditValue.ToString() + "')";
            }
            if (dtpReceivedDateTo.EditValue != null)
            {
                condition += " AND DF.RECEIVED_DATE <= CONVERT(DATETIME,'" + dtpReceivedDateTo.EditValue.ToString() + "')";
            }
            if (string.IsNullOrEmpty(txtDeliveryPerson.Text) == false)
            {
                condition += " AND DF.DELIVERY_PERSON = '" + txtDeliveryPerson.Text + "' ";
            }
            if (string.IsNullOrEmpty(cmbStatus.Text) == false && cmbStatus.SelectedValue != null)
            {
                condition += " AND DF.STATUS = '" + cmbStatus.SelectedValue.ToString() + "'";
            }
            if (!mCanQueryOtherPerson)
            {
                condition += " AND D.DELIVERY_PERSON = '" + CommonParameter.CurrentUser.PersonId + "' ";
            }
            return condition;
        }
        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "DF.FEEDBACK_CODE DESC";
            DataTable dt = SalesDeliveryFeedbackBs.GetByCondition(condition, orderBy, rowCount);
            gvMain.DataSource = dt;
        }
        private void ResetButtonsEnable()
        {
            btnModify.Enabled = false;
            btnDelete.Enabled = false;
            btnSubmit.Enabled = false;
            btnApprove.Enabled = false;
            if (mSelectedFeedback == null)
            {
                return;
            }
            if (mSelectedFeedback.Status == NormalStatus.STATUS01.ToString()
                || mSelectedFeedback.Status == NormalStatus.STATUS02.ToString()
                || mSelectedFeedback.Status == NormalStatus.STATUS04.ToString())
            {
                btnModify.Enabled = true;
                btnDelete.Enabled = true;
                btnApprove.Enabled = true;
            }
        }
        #endregion

        #region events
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            mCanQueryOtherPerson = UICommon.UICommonFunctions.CheckPersonRight(RoleCategory.销售);
            BindComboBox();
            BindDataGrid();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedFeedback = new SalesDeliveryFeedbackEntity(dr);
            ResetButtonsEnable();
        }

        protected override void Click_View()
        {
            if (mSelectedFeedback == null)
            {
                MessageBox.Show("请选择一个送货回馈单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainDeliveryFeedback frm = new frmMaintainDeliveryFeedback(mSelectedFeedback);
            frm.ThisActionType = ActionType.View;
            frm.ShowDialog();
        }

        protected override void Click_Delete()
        {
            if (mSelectedFeedback == null)
            {
                MessageBox.Show("请选择一个送货回馈单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否删除选中送货回馈单？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                mSelectedFeedback.Status = NormalStatus.STATUS05.ToString();
                mSelectedFeedback.LastModifiedBy = CommonParameter.CurrentUser.PersonId;
                mSelectedFeedback.LastModifiedDate = DateTime.Now;
                bool successful = SalesDeliveryFeedbackBs.UpdateSalesDeliveryFeedback(mSelectedFeedback);
                if (successful)
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        protected override void Click_Modify()
        {
            if (mSelectedFeedback == null)
            {
                MessageBox.Show("请选择一个送货回馈单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainDeliveryFeedback frm = new frmMaintainDeliveryFeedback(mSelectedFeedback);
            frm.ThisActionType = ActionType.Modify;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }
        #endregion


    }
}
